package com.zhhq.service.impl;

import com.zhhq.enums.ResultEnum;
import com.zhhq.exception.CustomException;
import com.zhhq.mapper.RoleMapper;
import com.zhhq.mapper.UserMapper;
import com.zhhq.model.User;
import com.zhhq.service.IUserService;
import com.zhhq.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by HeChaobin on 2017-06-01.
 */
@Service("CustomUserService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

//    @Override
//    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//        User user = userMapper.getUserByLoginName(s);
//        if (user == null) {
//            throw new BadCredentialsException("用户不存在");
//        }
//
//        // 获取权限
//        List<Role> roleList=roleMapper.selectUserRole(user.getId());
//        user.addAuthorities(roleList);
//
//        return user;
//    }

    @Override
    public User getUser(Long userID) {
        return null;
    }

    @Override
    public User login(String loginName, String password) {
        try {
            password = MD5Util.encode(password);

        } catch (Exception ex) {

        }
        User user = userMapper.login(loginName, password);
        if (user == null) {
            user = getUserByLoginName(loginName);
            if (user!=null){
                throw new CustomException(ResultEnum.PWDERROR);
            }
            else {
                throw new CustomException(ResultEnum.USERNOTEXISTS);
            }
        }

        return user;
    }

    @Override
    public User addUser(User user) {
        // 检测用户是否存在
        User tempUser = getUserByLoginName(user.getLoginname());
        if (tempUser != null) {
            // 登录名已存在，无法注册
            throw new CustomException(ResultEnum.USEREXISTS);
        }
        // 设置用户的具体信息
        try {
            String pw = MD5Util.encode(user.getPassword());
            user.setPassword(pw);
        } catch (Exception ex) {

        }

        userMapper.insert(user);

        return user;
    }

    @Override
    public User getUserByLoginName(String loginname) {
        return userMapper.getUserByLoginName(loginname);
    }

    @Override
    public void deleteUser(String loginName) {
        // 检测用户是否存在
        User tempUser = getUserByLoginName(loginName);
        if (tempUser == null) {
            throw new CustomException(ResultEnum.USERNOTEXISTS);
        }

        userMapper.deleteByLoginName(loginName);
    }

    @Override
    public void updateUser(User user) {

        // 检测用户是否存在
        User tempUser = getUserByLoginName(user.getLoginname());
        if (tempUser == null) {
            throw new CustomException(ResultEnum.USERNOTEXISTS);
        }
        try {
            user.setPassword(MD5Util.encode(user.getPassword()));

        } catch (Exception ex) {

        }

        userMapper.updateByLoginName(user);
    }

    @Override
    public void updatePassword(String loginName, String password) {
        // 检测用户是否存在
        User tempUser = getUserByLoginName(loginName);
        if (tempUser == null) {
            throw new CustomException(ResultEnum.USERNOTEXISTS);
        }
        try {
            password=MD5Util.encode(password);

        } catch (Exception ex) {

        }

        userMapper.updatePassword(loginName,password);
    }
}